package com.gulimall.coupon.service.impl;

import com.common.to.MemberPrice;
import com.common.to.SkuReduceTo;
import com.gulimall.coupon.dao.SkuLadderDao;
import com.gulimall.coupon.entity.MemberPriceEntity;
import com.gulimall.coupon.entity.SkuLadderEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.gulimall.coupon.dao.SkuFullReductionDao;
import com.gulimall.coupon.entity.SkuFullReductionEntity;
import com.gulimall.coupon.service.SkuFullReductionService;

import javax.annotation.Resource;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Resource
    private SkuLadderDao skuLadderDao;
    @Resource
    private MemberPriceServiceImpl memberPriceServiceImpl;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveFeign(SkuReduceTo skuReduceTo) {
        // sku 的优惠、满减信息  sms_sku_ladder、sms_sku_full_reduction、sms_member_price
        // 1、添加sms_sku_ladder
        if(skuReduceTo.getFullCount() > 0){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReduceTo,skuLadderEntity);
            skuLadderEntity.setAddOther(skuReduceTo.getCountStatus());
            skuLadderDao.insert(skuLadderEntity);
        }

        // 2、添加 sms_sku_full_reduction
        if(skuReduceTo.getFullPrice().compareTo(new BigDecimal("0")) == 1){
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReduceTo,skuFullReductionEntity);
            this.save(skuFullReductionEntity);
        }

        // 3、添加 sms_member_price
        List<MemberPrice> memberPriceList = skuReduceTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntityList = memberPriceList.stream().map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
            memberPriceEntity.setSkuId(skuReduceTo.getSkuId());
            memberPriceEntity.setAddOther(1);
            return memberPriceEntity;
        }).filter(memberPrice->memberPrice.getMemberPrice().compareTo(new BigDecimal("0")) == 1).collect(Collectors.toList());
        memberPriceServiceImpl.saveBatch(memberPriceEntityList);
    }

}